const Router = require('koa-router')
const router = Router({
    prefix: '/api'
})
const fs = require('fs');
const path = require('path')

let routerFiles =  fs.readdirSync(__dirname)
let routeFiles = routerFiles.filter(f => {
    return f.endsWith('.js') && !f.startsWith('index')
}, routerFiles);

console.log('init router ...')
for (let f of routeFiles) {
    console.log(`import router from file ${f}...`)
    let name = f.substring(0, f.length - 3);
    let filePath = path.join(__dirname, f);
    let r = require(filePath).default;

    router.use(r.routes(), r.allowedMethods());
}

export default router
